#Tales of Pirates (2) LGO parser

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class TalesOfPirates_LGO(Model3D):
    '''Replace 'TalesOfPirates_LGO' with a suitable name of your choice. Try to follow
    the standard: GameName_FormatExt'''
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(TalesOfPirates_LGO,self).__init__("LGO")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_material(self, filename):
        
        self.create_material(0)
        self.add_material_name(0, filename)
        self.add_texture_name(0, filename + ".bmp")
        
    def parse_vertices(self, meshName, numVerts):
        
        for i in xrange(numVerts):
            vx, vz, vy = self.inFile.read_float(3)
            
            vx *= -1
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
        
        for i in xrange(numVerts):
            nx, ny, nz = self.inFile.read_float(3)
            self.add_vert_normals(meshName, i, [nx, ny, nz])            
            
        for i in xrange(numVerts):
            tu, tv = self.inFile.read_float(2)           
            self.add_vert_uv(meshName, i, [tu, tv])

    def parse_faces(self, meshName, numFaces):
        
        #faces = self.inFile.read_long(numFaces*3)
        #temp = list(faces[0:2])
        #faceNum = 0
        #print numFaces
        #for i in xrange(2, numFaces*3):
            #temp.append(faces[i])
            #if temp[0] != temp[1] and temp[1] != temp[2] and temp[2] != temp[0]:
                #self.create_face(meshName, faceNum)
                #self.add_face_verts(meshName, faceNum, temp)
                #self.add_face_material(meshName, faceNum, 0)
                #faceNum +=1
            #temp.pop(0)
        faceNum = 0
        for i in xrange(numFaces):
            v1, v2, v3 = self.inFile.read_long(3)
            if v1 == v2 or v2 == v3 or v3 == v1:
                print v1, v2, v3
            else:
                self.create_face(meshName, faceNum)
                self.add_face_verts(meshName, faceNum, [v1, v2, v3])
                self.add_face_material(meshName, faceNum, 0)
                faceNum +=1
        
    def parse_matAnim(self, size):
        
        self.inFile.seek(size, 1)
        
    def parse_unk2(self, numVerts):
        '''May appear after vertex sectino'''
        
        for i in range(numVerts):
            self.inFile.seek(20, 1)
        self.inFile.read_long(3)
        
    def parse_unk3(self):
        '''Appears after the face section'''
 
        unkType = self.inFile.read_long()
        if unkType == 0x11:
            print "type 0x11"
            count = self.inFile.read_long()
            for i in range(count):
                self.inFile.read_long() #index?
                self.inFile.read_float(16)
                self.inFile.seek(72,1) #null
            self.inFile.seek(96, 1)
        elif unkType == 0x20:
            print "type 0x20"
        else:
            print "Unknown type", unkType
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        fsize = self.inFile.filesize()
        meshName = "obj"
        self.create_object(meshName)
        
        #header
        version = self.inFile.read_string(4)
        unk1 = self.inFile.read_long()
        self.inFile.seek(8, 1) #padding?
        localMatrix = self.inFile.read_float(16)
        self.inFile.read_long(4)
        self.inFile.read_char(8)
        matSize, meshSize, helpSize, animSize = self.inFile.read_ulong(4)
        self.parse_matAnim(matSize)
        
        
        #??
        unk2, unk3, numVerts = self.inFile.read_long(3)
        numFaces = self.inFile.read_long() / 3
        var1, var2, var3, var4 = self.inFile.read_long(4)
        
        self.inFile.seek(100, 1)
        
        #unk section
        if var4 == 4:
            self.inFile.seek(28, 1)
        elif var4 == 5:
            self.inFile.seek(36, 1)
        elif var4 == 6:
            self.inFile.seek(44, 1)
        
        print numVerts, numFaces, self.inFile.tell()
        self.parse_vertices(meshName, numVerts)
        
        print "%d, %d, %d, %d" %(var1, var2, var3, var4)
        print "var4 is", var4
        if var4 == 5:
            self.inFile.seek(numVerts * 4, 1)
        elif var4 == 4:
            pass
        elif var1 == 1:
            self.parse_unk2(numVerts)
        
        
        else:
            print "unknown var1:", var1
        self.parse_faces(meshName, numFaces)
        
def read_file(path):
    '''Read the file'''
    
    filename, ext = os.path.splitext(os.path.basename(path))
    openFile = StructReader(open(path, 'rb'))
    obj = TalesOfPirates_LGO(inFile=openFile)
    obj.parse_material(filename)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "\x50\x10\x00\x00", "LGO", "Tales of Pirates LGO"

if __name__ == '__main__':
    
    file1 = "0000000003.lgo"
    file2= "01010056.lgo"
    obj = read_file(file1)